package cn.gson.boot.model.service;

import cn.gson.boot.model.dao.PurchaseReturnDao;
import cn.gson.boot.model.dao.ReturnDetailsDao;
import cn.gson.boot.model.dao.ShenHeDao;
import cn.gson.boot.model.mapper.PurchaseReturnMapper;
import cn.gson.boot.model.pojos.*;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Service
@Transactional
public class PurchaseReturnService {
    @Autowired
    PurchaseReturnMapper purchaseReturnMapper;
    @Autowired
    PurchaseReturnDao purchaseReturnDao;
    @Autowired
    ReturnDetailsDao returnDetailsDao;
    @Autowired
    ShenHeDao shenHeDao;

    //查询单号
    public String PurchaseOrder() {
        SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMdd");
        Date d = new Date();
        String code = purchaseReturnMapper.PurchaseReturnOrder();
        if(null==code || "".equals(code)) {
            code = "CGTH"+sdf.format(d)+"1";
        }else {
            int zhi = Integer.parseInt(code)+1;
            code = "CGTH"+sdf.format(d)+ zhi +"";
        }
        return code;
    }

    //查询所有采购单
    public List<Purchase> purchaseList() {
        return purchaseReturnMapper.purchaseList();
    }

    //查询采购详情
    public List<Purchase> selectPurchse(Integer purchaseId) {
        return purchaseReturnMapper.selectPurchse(purchaseId);
    }

    //查询采购退货单状态
    public String purchaseReturnStatus(Integer purReturnId) {
        return purchaseReturnMapper.purchaseReturnStatus(purReturnId);
    }

    //新增采购退货单
    public void addPurchaseReturn(PurchaseReturn purchaseReturn, List purList) {
        JSONArray json = JSONArray.parseArray(String.valueOf(purList));
        if(purchaseReturn.getPurReturnId()==0) {
            purchaseReturn.setPurReturnStatus("待审核");
            purchaseReturn.setPurReturnCk("未出库");
            String Nowtime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());//一个string类型的当前时间
            java.sql.Date mysqldate = java.sql.Date.valueOf(Nowtime);
            purchaseReturn.setPurReturnDate(mysqldate);
            purchaseReturn.setPurReturnSc(0);
            purchaseReturnDao.save(purchaseReturn);
            Shenhe shenhe = new Shenhe();
            shenhe.setShTabid(purchaseReturn.getPurReturnId());
            shenhe.setShShouming("采购退货");
            shenhe.setShZhuangtai("待审核");
            shenhe.setEmp(null);
            shenhe.setChuangJiantime(new Timestamp(new Date().getTime()));
            shenhe.setShenqingPeople(purchaseReturn.getPurReturnMan());
            shenHeDao.save(shenhe);
            ReturnDetails returnDetails = null;
            JSONObject ob = null;
            Productt productt = null;
            for (int i = 0; i < json.size(); i++) {
                ob = (JSONObject) json.get(i);//得到json对象
                Integer retuendeailsId = ob.getInteger("detailsId");
                Integer productId = ob.getInteger("productId");
                Integer purNumber = ob.getInteger("purNumber");
                BigDecimal purPrice = ob.getBigDecimal("purPrice");
                productt = new Productt();
                productt.setProducttId(productId);
                returnDetails = new ReturnDetails();
                if (retuendeailsId != null) {
                    returnDetails.setReturnDeailsId(retuendeailsId);
                }
                returnDetails.setReturnDetailsNumber(purNumber);
                returnDetails.setReturnDetailsPrice(new BigDecimal(String.valueOf(purPrice.multiply(BigDecimal.valueOf(purNumber)))));
                returnDetails.setProducttId(productt);
                returnDetails.setPurchaseReturnId(purchaseReturn);
                returnDetailsDao.save(returnDetails);
            }
        }else{
            purchaseReturn.setPurReturnDate(purchaseReturn.getPurReturnDate());
            purchaseReturnDao.save(purchaseReturn);
            ReturnDetails returnDetails = null;
            JSONObject ob = null;
            Productt productt = null;
            returnDetailsDao.deleteAll(purchaseReturnMapper.selectPurchaseReturnDetails(purchaseReturn.getPurReturnId()));
            for (int i = 0; i < json.size(); i++) {
                ob = (JSONObject) json.get(i);//得到json对象
                Integer retuendeailsId = ob.getInteger("detailsId");
                Integer productId = ob.getInteger("productId");
                Integer purNumber = ob.getInteger("purNumber");
                BigDecimal purPrice = ob.getBigDecimal("purPrice");
                productt = new Productt();
                productt.setProducttId(productId);
                returnDetails = new ReturnDetails();
                if (retuendeailsId != null) {
                    returnDetails.setReturnDeailsId(retuendeailsId);
                }
                returnDetails.setReturnDetailsNumber(purNumber);
                returnDetails.setReturnDetailsPrice(new BigDecimal(String.valueOf(purPrice.multiply(BigDecimal.valueOf(purNumber)))));
                returnDetails.setProducttId(productt);
                returnDetails.setPurchaseReturnId(purchaseReturn);
                returnDetailsDao.save(returnDetails);
            }
        }
    }

    //分页查询
    public List<PurchaseReturn> selectPurchaseReturn(String search, String type) {
        return purchaseReturnMapper.selectPurchaseReturn(search,type);
    }

    //删除采购退货单
    public void delpurchaseReturn(Integer purReturnId) {
        PurchaseReturn purchaseReturn = purchaseReturnDao.findById(purReturnId).get();
        purchaseReturn.setPurReturnSc(1);
        purchaseReturnDao.save(purchaseReturn);
    }

    public List<ReturnDetails> selectPurchaseReturnDetails(Integer purReturnId) {
        return purchaseReturnMapper.selectPurchaseReturnDetails(purReturnId);
    }

    //查询采购单是否已经生成了退货单
    public List<PurchaseDetails> selectPurchaseReturnStatus(Integer purchaseId) {
        return purchaseReturnMapper.selectPurchaseReturnStatus(purchaseId);
    }


    //根据id查到对象
    public PurchaseReturn selectPurchaseReturnByPurreturnId(Integer purreturnId){
        return purchaseReturnMapper.selectPurchaseReturnByPurreturnId(purreturnId);
    }
}
